package xuegao.study.mall.admin.service.pms.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import xuegao.study.mall.repo.dao.pms.PmsBrandDao;
import xuegao.study.mall.common.model.pms.PmsBrand;
import xuegao.study.mall.common.param.pms.PmsBrandParam;
import xuegao.study.mall.admin.service.pms.PmsBrandService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PmsBrandServiceImpl implements PmsBrandService {

    @Autowired
    private PmsBrandDao pmsBrandDao;

    @Override
    public List<PmsBrand> listBrand(String keyword, Integer showStatus, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> params = new HashMap<>();
        if (!StringUtils.isEmpty(keyword)) {
            params.put("name", keyword);
        }
        if (showStatus != null) {
            params.put("showStatus", showStatus);
        }
        return pmsBrandDao.selectByParams(params);
    }

    @Override
    @Transactional
    public int update(Long id, PmsBrandParam param) {
        PmsBrand brand = new PmsBrand();
        BeanUtils.copyProperties(param, brand);
        brand.setId(id);
        // todo 需要更新商品中的品牌名称
        return pmsBrandDao.updateByParams(brand);
    }
}
